package link.anyauto.weclass;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import link.anyauto.weclass.adapter.OneTypeItemSource;
import link.anyauto.weclass.base.App;
import link.anyauto.weclass.base.BaseVMAdapter;
import link.anyauto.weclass.demos.actionbar.ActionBarActivity;
import link.anyauto.weclass.demos.viewpager.PagerActivity;
import link.anyauto.weclass.demos.viewpager.PagerTabActivity;
import link.anyauto.weclass.util.ToastUtil;

/**
 * Created by discotek on 17-2-10.
 */

public class DirectoryVM extends BaseVMAdapter {

    public OneTypeItemSource<DirectoryItem> dirs = new OneTypeItemSource<>();
    long lastClick;

    public DirectoryVM() {
        dirs.itemViewId = R.layout.directory_item;
        prepareItems();
        title(R.string.app_name).navLogo(R.mipmap.ic_launcher);
    }

    public void back() {
        if (System.currentTimeMillis() - lastClick > 2500) {
            ToastUtil.toast(R.string.click_again_exit);
            lastClick = System.currentTimeMillis();
        } else {
            super.back();
        }
    }

    void prepareItems() {
        DirectoryItem dir = new DirectoryItem();
        dir.name = App.getStr(R.string.view_pager_demo);
        dir.act = PagerActivity.class;
        dirs.items.add(dir);

        dir = new DirectoryItem();
        dir.name = App.getStr(R.string.view_pager_tab_demo);
        dir.act = PagerTabActivity.class;
        dirs.items.add(dir);

        dir = new DirectoryItem();
        dir.name = App.getStr(R.string.app_bar_demo);
        dir.act = ActionBarActivity.class;
        dirs.items.add(dir);
    }

    public void select(int pos) {
        activity.startActivity(new Intent(activity, dirs.items.get(pos).act));
    }

    @Override
    public boolean createOptionsMenu(MenuInflater inflater, Menu menu) {
        inflater.inflate(R.menu.menu_directory, menu);
        return true;
    }

    @Override
    public boolean optionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return false;
    }
}
